var page = 0,
    num = 0;
function doAjax() {
    page++;
    $.ajax({
        url:'/integralGoods/search.json',
        dataType:'json',
        data:{
            companyCode:getItem('companyCode'),
            goodsTypeValue:2,
            page:page,
        },
        beforeSend:function(){
            if(page==1){
                $('<div class="loading"><img src="../phoneImg/jiazaizhong.gif"/></div>').appendTo($('.list_box'));
            }
        },
        success:function (data) {
            if(data.code==1){
                if(page==1){
                    $('.list_box').empty();
                    if(data.datas.datas.length==0){
                        mui('#refreshContainer').pullRefresh().disablePullupToRefresh();
                        $('<div class="kong"><img src="../phoneImg/kong.png" alt="暂无数据"></div>').appendTo($('.list_box'));
                    }
                }
                var inteData = data.datas.datas;
                for(var i=0;i<inteData.length;i++){
                    $('<div class="item_box mui-clearfix" goodsId="'+inteData[i].periodsId+'">\n' +
                        '<div class="img"><img src="'+inteData[i].mainImg.split(',')[0]+'" alt=""></div>\n' +
                        '<div class="goods">\n' +
                        '<div class="title">'+inteData[i].name+'</div>\n' +
                        '<div class="mui-progressbar mui-progressbar-in" data-progress="'+inteData[i].baifen+'" id="progess'+inteData[i].periodsId+'"><span></span></div>'+
                        '<div class="price">总需'+inteData[i].totalNum+'<span class="quondam">剩余<b>'+(inteData[i].totalNum-inteData[i].nowNum)+'</b></span></div>\n' +
                        '<div class="join"><img src="../phoneImg/JOIN2.jpg" alt=""></div>\n' +
                        '</div></div>').appendTo($('.list_box'))
                    var progressbar1 = mui('#progess'+inteData[i].periodsId+'');
                    var progressBar = inteData[i].baifen;
                    mui(progressbar1).progressbar().setProgress(progressBar)
                }
                num = inteData.length;

            }
            mui('body').on('tap','.item_box',function () {
                window.location.href="integral_goods.html?goodsId="+$(this).attr('goodsId')+'&goodsTypeValue=2';
            })
        }
    })
    return num;
}

doAjax();

//上拉加载
mui.init({
    pullRefresh : {
        container:"#refreshContainer",//待刷新区域标识，querySelector能定位的css选择器均可，比如：id、.class等
        down: {
            contentdown : "下拉可以刷新",//可选，在下拉可刷新状态时，下拉刷新控件上显示的标题内容
            contentover : "释放立即刷新",//可选，在释放可刷新状态时，下拉刷新控件上显示的标题内容
            contentrefresh : "正在刷新...",//可选，正在刷新状态时，下拉刷新控件上显示的标题内容
            callback: function () {
                window.location.reload();
                mui('#refreshContainer').pullRefresh().endPulldownToRefresh();
            }
        },
        up : {
            contentrefresh : "正在加载...",//可选，正在加载状态时，上拉加载控件上显示的标题内容
            contentnomore:'我是有底线的',//可选，请求完毕若没有更多数据时显示的提醒内容；
            callback :function(){
                setTimeout(function () {
                    var numResult;
                    numResult = doAjax();
                    if (numResult < 20) {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(true);
                    } else {
                        mui('#refreshContainer').pullRefresh().endPullupToRefresh(false);
                    }
                }, 1000);
            } //必选，刷新函数，根据具体业务来编写，比如通过ajax从服务器获取新数据；
        }
    }
});